﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TvTipper.Controls
{
    public partial class RankControl : UserControl
    {
        private int m_value;
        private PictureBox[] m_boxes = new PictureBox[5];
        private Bitmap m_on = new Bitmap(TvTipper.Properties.Resources.star);
        private Bitmap m_off = new Bitmap(TvTipper.Properties.Resources.star_bw);

        public RankControl()
        {
            InitializeComponent();
            m_boxes[0] = m_box1;
            m_boxes[1] = m_box2;
            m_boxes[2] = m_box3;
            m_boxes[3] = m_box4;
            m_boxes[4] = m_box5;
        }

        [Bindable(true)] 
        public int Value
        {
            get { return m_value; }
            set
            {
                if (m_value != value)
                {
                    if (value > 5 || value < 0)
                    {
                        throw new ArgumentException("The value must be between 0 to 5");
                    }
                }

                m_value = value;

                for (int i = 0; i < m_value; i++)
                {
                    m_boxes[i].Image = m_on;
                }

                for (int i = m_value; i < 5; i++)
                {
                    m_boxes[i].Image = m_off;
                }
            }
        }

        private void OnStarClicked(object sender, EventArgs e)
        {
            var im = (PictureBox)sender;
            if (ValueChanged != null)
            {
                int val = Int32.Parse(im.Tag.ToString());
                ValueChanged(this, new RankEventArgs() { NewValue = val });
            }
        }

        public event EventHandler<RankEventArgs> ValueChanged;

        public class RankEventArgs : EventArgs
        {
            public int NewValue { get; set; }
        }
    }
}
